from django.db.models import Q
from django.shortcuts import render
from rest_framework.filters import OrderingFilter
from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ModelViewSet

from goods.goodsser import CateSerializer, CateSer, OriginSerializer, GoodsSerializer, AssessSerializer
from goods.models import CateModel, GoodsModel, OriginModel, AssessModel


# Create your views here.

# 首页展示分类信息
class CateView(APIView):
    def get(self, request):
        all_cate = CateModel.objects.filter(parent=None, is_home=True)
        cate_ser = CateSerializer(instance=all_cate, many=True)
        return Response(cate_ser.data)


# 首页展示商品信息
class GetCateView(APIView):
    def get(self, request):
        # 获取所有父级分类下的前三条商品数据
        all_cate = CateModel.objects.filter(parent=None, is_home=True).order_by('id')
        cate_ser = CateSerializer(instance=all_cate, many=True)
        for cate in cate_ser.data:
            some_cate = CateModel.objects.filter(parent=cate['id']).values_list('id', flat=True)
            goods = GoodsModel.objects.filter(cate__in=some_cate, is_home=True).order_by('id').values()[:3]
            cate['goods'] = goods
        return Response({'message': "ok", 'data': cate_ser.data, 'code': 200})


# 跳转后显示主分类(水果类/蔬菜类/肉类....)
class KindView(APIView):
    def get(self, request):
        kind_cate = CateModel.objects.filter(parent=None, is_home=True).order_by('id')
        ser = CateSer(kind_cate, many=True)
        return Response({'message': "ok", 'data': ser.data, 'code': 200})


# 跳转后展示子分类
class ChildCate(APIView):
    def get(self, request):
        id = int(request.query_params.get('id'))
        if id == 0:
            child = CateModel.objects.exclude(parent=None, is_home=False).order_by('id').values()[3:]
        else:
            child = CateModel.objects.filter(parent=id, is_home=True).order_by('id')
        ser = CateSer(child, many=True)
        return Response({'message': "ok", 'data': ser.data, 'code': 200})


# 获取产地
class OriginView(ModelViewSet):
    queryset = OriginModel.objects.all()
    serializer_class = OriginSerializer
    lookup_field = 'id'
    lookup_url_kwarg = 'id'


class Pagination(PageNumberPagination):
    page_size = 4
    page_size_query_param = 'size'
    page_query_param = 'page'


class GoodsListView(ListAPIView):
    queryset = GoodsModel.objects.all()
    serializer_class = GoodsSerializer
    pagination_class = Pagination
    filter_backends = [OrderingFilter]
    ordering_fields = ['g_price', 'sales']

    def get_queryset(self):
        query = Q()
        g_name = self.request.query_params.get('g_name')
        father = self.request.query_params.get('father')
        son = self.request.query_params.get('son')
        storage = self.request.query_params.get('storage')
        origin = self.request.query_params.get('origin')
        is_have = self.request.query_params.get('is_have')
        if g_name:
            query &= Q(g_name__contains=g_name)
        if int(son) != 0:
            query &= Q(cate=son)
        if int(father) > 0 and int(son) == 0:
            all_cate = CateModel.objects.filter(parent=father).values()
            child = []
            for i in all_cate:
                child.append(i['id'])
            query &= Q(cate__in=child)
        if int(storage) > 0:
            query &= Q(storage=storage)
        if int(origin) != 0:
            query &= Q(origin=origin)
        if is_have == "true":
            query &= Q(is_have=True)
        return GoodsModel.objects.filter(query)


# 商品详情
class GetGoodsView(APIView):
    def get(self, request):
        id = request.query_params.get('id')
        goods = GoodsModel.objects.filter(id=id).first()
        ser = GoodsSerializer(goods)
        return Response({'message': "ok", 'data': ser.data, 'code': 200})


# 商品评价
class AssessView(ListAPIView):
    queryset = AssessModel.objects.all()
    serializer_class = AssessSerializer
    pagination_class = Pagination

    def get_queryset(self):
        id = self.request.query_params.get('id')
        return AssessModel.objects.filter(goods=id)
